package com.example.myapplication.suspended_frame;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Looper;
import android.view.View;

/**
 * Create By TreeRoot
 * Create Time: 2024/07/26 03:46
 * Class Name: DreamIconPaint
 * Description:
 * 绘制图标
 * 绘制头部、左眼和右眼
 * set get方法看起来没有使用，实际上被动画的反射调用，不要删除
 * 初始头部 48 * 48   扁了之后 60 * 48
 * 眼睛 10 * 16  眯眼 10 * 4
 * @author TreeRoot
 */
@SuppressWarnings("unused")
public class DrawIconPaint extends View {

    private final String TAG = "CustomDrawableView";
    private int state;
    private boolean isVisible = true;

    // 画笔对象，用于绘制头部、左眼和右眼
    private Paint headerPaint;
    private Paint leftEyePaint;
    private Paint rightEyePaint;

    //页面透明度
    private int alpha = 256;

    // 头部初始位置
    private float headerXStart = 32;
    private float headerXEnd = 32;
    private float headerYStart = 32;
    private float headerYEnd = 0;
    private float headStrokeWidth = 48;

    // 左眼的初始位置
    private float leftEyeXStart = 22;
    private float leftEyeXEnd = 22;
    private float leftEyeYStart = 25;
    private float leftEyeYEnd = 31;
    private float leftStrokeWidth = 10;

    // 右眼的初始位置
    private float rightEyeXStart = 42;
    private float rightEyeXEnd = 42;
    private float rightEyeYStart = 25;
    private float rightEyeYEnd = 31;
    private float rightStrokeWidth = 10;

    // 定义颜色常量
    public final int GrayBlack = 0xFF444F5A;
    public final int RedPink = 0xFFF6416C;
    public final int White = 0xFFFFFFFF;

    private final Handler handler = new Handler(Looper.getMainLooper());

    private static volatile DrawIconPaint instance;

    private DrawIconPaint(Context context) {
        super(context);
        init();
    }

    public static DrawIconPaint getInstance(Context context) {
        if (instance == null) {
            synchronized (DrawIconPaint.class) {
                if (instance == null) {
                    instance = new DrawIconPaint(context);
                }
            }
        }
        return instance;
    }


    /**
     * 初始化画笔和延迟 2 秒后开始动画
     */
    private void init() {

        // 初始化头部画笔
        headerPaint = new Paint();
        headerPaint.setColor(GrayBlack);
        headerPaint.setStyle(Paint.Style.FILL);
        headerPaint.setAntiAlias(true);

        // 初始化左眼画笔
        leftEyePaint = new Paint();
        leftEyePaint.setColor(RedPink);
        leftEyePaint.setStyle(Paint.Style.FILL);
        leftEyePaint.setAntiAlias(true);

        // 初始化右眼画笔
        rightEyePaint = new Paint();
        rightEyePaint.setColor(RedPink);
        rightEyePaint.setStyle(Paint.Style.FILL);
        rightEyePaint.setAntiAlias(true);
        state = StateController.INIT;
        handler.postDelayed(this::initToRightEyeClose, 1000);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 自定义的宽度和高度值
        int customHeight = 64;
        int customWidth = 64;
        //自定义透明度
        setAlpha(alpha);
        setMeasuredDimension(customWidth, customHeight);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isVisible) {
            // 绘制头部
            headerPaint.setStyle(Paint.Style.STROKE);
            headerPaint.setStrokeWidth(headStrokeWidth);
            headerPaint.setStrokeCap(Paint.Cap.ROUND);
            canvas.drawLine(headerXStart, headerYStart, headerXEnd, headerYStart + 0.01F, headerPaint);

            // 绘制左眼
            leftEyePaint.setStyle(Paint.Style.STROKE);
            leftEyePaint.setStrokeWidth(leftStrokeWidth);
            leftEyePaint.setStrokeCap(Paint.Cap.ROUND);
            canvas.drawLine(leftEyeXStart, leftEyeYStart, leftEyeXEnd, leftEyeYEnd, leftEyePaint);

            // 绘制右眼
            rightEyePaint.setStyle(Paint.Style.STROKE);
            rightEyePaint.setStrokeWidth(rightStrokeWidth);
            rightEyePaint.setStrokeCap(Paint.Cap.ROUND);
            canvas.drawLine(rightEyeXStart, rightEyeYStart, rightEyeXEnd, rightEyeYEnd, rightEyePaint);
        }
    }

    /**
     * 隐藏
     */
    public void hide() {
        isVisible = false;
        invalidate(); // 重绘视图
    }

    /**
     * 显示
     */
    public void show() {
        isVisible = true;
        invalidate(); // 重绘视图
    }

    /**
     * 初始化状态到右眼闭合状态
     */
    private void initToRightEyeClose() {
        // 右眼画笔变化
        ValueAnimator rightStrokeWidthAnimator = ValueAnimator.ofFloat(10, 5);
        rightStrokeWidthAnimator.setDuration(200);
        rightStrokeWidthAnimator.addUpdateListener(animation -> {
            rightStrokeWidth = (float) animation.getAnimatedValue();
            invalidate();
        });
        rightStrokeWidthAnimator.start();

        // 右眼 Y 轴变化
        ValueAnimator rightEyeYStartAnimator = ValueAnimator.ofFloat(25, 31, 28);
        rightEyeYStartAnimator.setDuration(200);
        rightEyeYStartAnimator.addUpdateListener(animation -> {
            rightEyeYStart = (float) animation.getAnimatedValue();
            invalidate();
        });
        rightEyeYStartAnimator.start();

        ValueAnimator rightEyeYEndAnimator = ValueAnimator.ofFloat(31, 25, 28);
        rightEyeYEndAnimator.setDuration(200);
        rightEyeYEndAnimator.addUpdateListener(animation -> {
            rightEyeYEnd = (float) animation.getAnimatedValue();
            invalidate();
        });
        rightEyeYEndAnimator.start();

        rightEyeYEndAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                // 右眼 X 轴变化
                ValueAnimator rightEyeXStartAnimator = ValueAnimator.ofFloat(42, 39);
                rightEyeXStartAnimator.setDuration(50);
                rightEyeXStartAnimator.addUpdateListener(animationXStart -> {
                    rightEyeXStart = (float) animationXStart.getAnimatedValue();
                    invalidate();
                });
                rightEyeXStartAnimator.start();

                ValueAnimator rightEyeXEndAnimator = ValueAnimator.ofFloat(42, 45);
                rightEyeXEndAnimator.setDuration(100);
                rightEyeXEndAnimator.addUpdateListener(animationXEnd -> {
                    rightEyeXEnd = (float) animationXEnd.getAnimatedValue();
                    invalidate();
                });
                rightEyeXEndAnimator.start();
            }
        });


        // 头部变化
        ValueAnimator headerXLeftAnimator = ValueAnimator.ofFloat(32, 33, 30);
        headerXLeftAnimator.setDuration(300);
        headerXLeftAnimator.addUpdateListener(animation -> {
            headerXStart = (float) animation.getAnimatedValue();
            invalidate();
        });
        headerXLeftAnimator.start();

        ValueAnimator headerXRightAnimator = ValueAnimator.ofFloat(32, 29, 35);
        headerXRightAnimator.setDuration(300);
        headerXRightAnimator.addUpdateListener(animation -> {
            headerXEnd = (float) animation.getAnimatedValue();
            invalidate();
        });
        headerXRightAnimator.start();

        headerXRightAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                postDelayed(DrawIconPaint.this::rightEyeCloseToOpen, 1000);
                state = StateController.RIGHT_EYE_CLOSE;
            }
        });

    }

    /**
     * 右眼闭合状态到右眼张开状态
     */
    private void rightEyeCloseToOpen() {
        // 右眼画笔变化
        ValueAnimator rightStrokeWidthAnimator = ValueAnimator.ofFloat(5, 10);
        rightStrokeWidthAnimator.setDuration(250);
        rightStrokeWidthAnimator.addUpdateListener(animation -> {
            rightStrokeWidth = (float) animation.getAnimatedValue();
            invalidate();
        });

        // 右眼 Y 轴变化
        ValueAnimator rightEyeYStartAnimator = ValueAnimator.ofFloat(28, 32, 25);
        rightEyeYStartAnimator.setDuration(200);
        rightEyeYStartAnimator.addUpdateListener(animation -> {
            rightEyeYStart = (float) animation.getAnimatedValue();
            invalidate();
        });

        ValueAnimator rightEyeYEndAnimator = ValueAnimator.ofFloat(28, 25, 31);
        rightEyeYEndAnimator.setDuration(200);
        rightEyeYEndAnimator.addUpdateListener(animation -> {
            rightEyeYEnd = (float) animation.getAnimatedValue();
            invalidate();
        });

        PropertyValuesHolder rightEyeXEndHolder = PropertyValuesHolder.ofFloat("rightEyeXStart", 39, 40);
        PropertyValuesHolder rightEyeXStartHolder = PropertyValuesHolder.ofFloat("rightEyeXEnd", 45, 40);
        ObjectAnimator rightX = ObjectAnimator.ofPropertyValuesHolder(this, rightEyeXStartHolder, rightEyeXEndHolder);
        rightX.setDuration(0);


        rightStrokeWidthAnimator.start();
        rightEyeYStartAnimator.start();
        rightEyeYEndAnimator.start();
        rightX.start();



        // 左眼 X 轴变化
        ValueAnimator leftEyeXStartAnimator = ValueAnimator.ofFloat(leftEyeXStart, 25,20);
        leftEyeXStartAnimator.setDuration(250);
        leftEyeXStartAnimator.addUpdateListener(animation -> {
            leftEyeXStart = (float) animation.getAnimatedValue();
            invalidate();
        });
        leftEyeXStartAnimator.start();

        ValueAnimator leftEyeXEndAnimator = ValueAnimator.ofFloat(leftEyeXEnd, 25, 20);
        leftEyeXEndAnimator.setDuration(250);
        leftEyeXEndAnimator.addUpdateListener(animation -> {
            leftEyeXEnd = (float) animation.getAnimatedValue();
            invalidate();
        });
        leftEyeXEndAnimator.start();

        rightX.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                postDelayed(DrawIconPaint.this::leftEyeToEyes, 1000);
            }
        });

    }

    /**
     * 左眼向左偏移状态到双眼向上偏移状态
     */
    private void leftEyeToEyes() {
        PropertyValuesHolder leftEyeXEndHolder = PropertyValuesHolder.ofFloat("leftEyeXStart", 20, 18);
        PropertyValuesHolder leftEyeXStartHolder = PropertyValuesHolder.ofFloat("leftEyeXEnd", 20, 18);
        ObjectAnimator leftX = ObjectAnimator.ofPropertyValuesHolder(this, leftEyeXEndHolder, leftEyeXStartHolder);
        leftX.setDuration(0);
        leftX.start();


        PropertyValuesHolder rightEyeXEndHolder = PropertyValuesHolder.ofFloat("rightEyeXStart", 40, 38);
        PropertyValuesHolder rightEyeXStartHolder = PropertyValuesHolder.ofFloat("rightEyeXEnd", 40, 38);
        ObjectAnimator rightX = ObjectAnimator.ofPropertyValuesHolder(this, rightEyeXStartHolder, rightEyeXEndHolder);
        rightX.setDuration(0);
        rightX.start();


        // 左眼 Y 轴变化
        ValueAnimator leftEyeYStartAnimator = ValueAnimator.ofFloat(leftEyeYStart, 20, 25, 20);
        leftEyeYStartAnimator.setDuration(250);
        leftEyeYStartAnimator.addUpdateListener(animation -> {
            leftEyeYStart = (float) animation.getAnimatedValue();
            invalidate();
        });
        leftEyeYStartAnimator.start();

        ValueAnimator leftEyeYEndAnimator = ValueAnimator.ofFloat(leftEyeYEnd, 26, 31, 26);
        leftEyeYEndAnimator.setDuration(250);
        leftEyeYEndAnimator.addUpdateListener(animation -> {
            leftEyeYEnd = (float) animation.getAnimatedValue();
            invalidate();
        });
        leftEyeYEndAnimator.start();


        // 右眼 Y 轴变化
        ValueAnimator rightEyeYStartAnimator = ValueAnimator.ofFloat(rightEyeYStart, 20, 25, 20);
        rightEyeYStartAnimator.setDuration(250);
        rightEyeYStartAnimator.addUpdateListener(animation -> {
            rightEyeYStart = (float) animation.getAnimatedValue();
            invalidate();
        });
        rightEyeYStartAnimator.start();

        ValueAnimator rightEyeYEndAnimator = ValueAnimator.ofFloat(rightEyeYEnd, 26, 31, 26);
        rightEyeYEndAnimator.setDuration(250);
        rightEyeYEndAnimator.addUpdateListener(animation -> {
            rightEyeYEnd = (float) animation.getAnimatedValue();
            invalidate();
        });
        rightEyeYEndAnimator.start();

        state = StateController.EYES_TO_LEFT;

        leftEyeYStartAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                postDelayed(DrawIconPaint.this::eyesToInit, 1000);
            }
        });
    }

    /**
     * 双眼向上偏移状态到初始化状态
     */
    private void eyesToInit() {


        PropertyValuesHolder leftEyeXEndHolder = PropertyValuesHolder.ofFloat("leftEyeXStart", 18, 22);
        PropertyValuesHolder leftEyeXStartHolder = PropertyValuesHolder.ofFloat("leftEyeXEnd", 18, 22);
        ObjectAnimator leftX = ObjectAnimator.ofPropertyValuesHolder(this, leftEyeXEndHolder, leftEyeXStartHolder);
        leftX.setDuration(0);
        leftX.start();


        PropertyValuesHolder rightEyeXEndHolder = PropertyValuesHolder.ofFloat("rightEyeXStart", 38, 42);
        PropertyValuesHolder rightEyeXStartHolder = PropertyValuesHolder.ofFloat("rightEyeXEnd", 38, 42);
        ObjectAnimator rightX = ObjectAnimator.ofPropertyValuesHolder(this, rightEyeXStartHolder, rightEyeXEndHolder);
        rightX.setDuration(0);
        rightX.start();

        // 右眼 Y 轴变化
        ValueAnimator rightEyeYStartAnimator = ValueAnimator.ofFloat(20, 25, 20, 25);
        rightEyeYStartAnimator.setDuration(150);
        rightEyeYStartAnimator.addUpdateListener(animation -> {
            rightEyeYStart = (float) animation.getAnimatedValue();
            invalidate();
        });
        rightEyeYStartAnimator.start();
        ValueAnimator rightEyeYEndAnimator = ValueAnimator.ofFloat(26, 31,26,31);
        rightEyeYEndAnimator.setDuration(150);
        rightEyeYEndAnimator.addUpdateListener(animation -> {
            rightEyeYEnd = (float) animation.getAnimatedValue();
            invalidate();
        });
        rightEyeYEndAnimator.start();


        // 左眼 Y 轴变化
        ValueAnimator leftEyeYStartAnimator = ValueAnimator.ofFloat(20, 25, 20, 25);
        leftEyeYStartAnimator.setDuration(150);
        leftEyeYStartAnimator.addUpdateListener(animation -> {
            leftEyeYStart = (float) animation.getAnimatedValue();
            invalidate();
        });
        leftEyeYStartAnimator.start();

        ValueAnimator leftEyeYEndAnimator = ValueAnimator.ofFloat(26, 31, 26, 31);
        leftEyeYEndAnimator.setDuration(150);
        leftEyeYEndAnimator.addUpdateListener(animation -> {
            leftEyeYEnd = (float) animation.getAnimatedValue();
            invalidate();
        });
        leftEyeYEndAnimator.start();





        // 头部变化
        ValueAnimator headerXLeftAnimator = ValueAnimator.ofFloat(30, 33,32);
        headerXLeftAnimator.setDuration(300);
        headerXLeftAnimator.addUpdateListener(animation -> {
            headerXStart = (float) animation.getAnimatedValue();
            invalidate();
        });
        headerXLeftAnimator.start();

        ValueAnimator headerXRightAnimator = ValueAnimator.ofFloat(35, 29,32);
        headerXRightAnimator.setDuration(250);
        headerXRightAnimator.addUpdateListener(animation -> {
            headerXEnd = (float) animation.getAnimatedValue();
            invalidate();
        });
        headerXRightAnimator.start();


        state = StateController.TO_INIT;

        headerXLeftAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                postDelayed(DrawIconPaint.this::initToRightEyeClose, 1000);
            }
        });
    }

    public float getHeaderXStart() {
        return headerXStart;
    }

    public void setHeaderXStart(float headerXStart) {
        this.headerXStart = headerXStart;
        invalidate();
    }

    public float getHeaderXEnd() {
        return headerXEnd;
    }

    public void setHeaderXEnd(float headerXEnd) {
        this.headerXEnd = headerXEnd;
        invalidate();
    }

    public float getHeadStrokeWidth() {
        return headStrokeWidth;
    }

    public void setHeadStrokeWidth(float headStrokeWidth) {
        invalidate();
        this.headStrokeWidth = headStrokeWidth;
    }

    public float getHeaderYStart() {
        return headerYStart;
    }

    public void setHeaderYStart(float headerYStart) {
        this.headerYStart = headerYStart;
        invalidate();
    }

    public float getLeftEyeXStart() {
        return leftEyeXStart;
    }

    public void setLeftEyeXStart(float leftEyeXStart) {
        this.leftEyeXStart = leftEyeXStart;
        invalidate();
    }

    public float getRightEyeXStart() {
        return rightEyeXStart;
    }

    public void setRightEyeXStart(float rightEyeXStart) {
        this.rightEyeXStart = rightEyeXStart;
        invalidate();
    }

    public float getLeftEyeYStart() {
        return leftEyeYStart;
    }

    public void setLeftEyeYStart(float leftEyeYStart) {
        this.leftEyeYStart = leftEyeYStart;
        invalidate();
    }

    public float getLeftEyeXEnd() {
        return leftEyeXEnd;
    }

    public void setLeftEyeXEnd(float leftEyeXEnd) {
        this.leftEyeXEnd = leftEyeXEnd;
    }

    public float getLeftEyeYEnd() {
        return leftEyeYEnd;
    }

    public void setLeftEyeYEnd(float leftEyeYEnd) {
        this.leftEyeYEnd = leftEyeYEnd;
    }

    public float getRightEyeYStart() {
        return rightEyeYStart;
    }

    public void setRightEyeYStart(float rightEyeYStart) {
        this.rightEyeYStart = rightEyeYStart;
        invalidate();
    }

    public float getHeaderYEnd() {
        return headerYEnd;
    }

    public void setHeaderYEnd(float headerYEnd) {
        this.headerYEnd = headerYEnd;
        invalidate();
    }

    public float getRightEyeXEnd() {
        return rightEyeXEnd;
    }

    public void setRightEyeXEnd(float rightEyeXEnd) {
        this.rightEyeXEnd = rightEyeXEnd;
        invalidate();
    }

    public float getRightEyeYEnd() {
        return rightEyeYEnd;
    }

    public void setRightEyeYEnd(float rightEyeYEnd) {
        this.rightEyeYEnd = rightEyeYEnd;
        invalidate();
    }

    public float getRightStrokeWidth() {
        return rightStrokeWidth;
    }

    public void setRightStrokeWidth(float rightStrokeWidth) {
        this.rightStrokeWidth = rightStrokeWidth;
        invalidate();
    }

    public void changeLeftEyeColor(int color) {
        leftEyePaint.setColor(color);
        invalidate();
    }

    public void changeRightEyeColor(int color) {
        rightEyePaint.setColor(color);
        invalidate();
    }
}
